package com.rcp.manager;/* Restaurant Control Program >> Restaurant Manager */


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Scanner;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Connection;

@WebServlet("/Nexus")
public class Nexus extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	public static Connection connection;
	
		
		public static String dbURL = "jdbc:mysql://cs3.calstatela.edu:3306/cs437group01";
		public static String dbName = "";
		public static String username = "cs437group01";
		public static String password = "VHj#dj!v";
		public static String driver = "com.mysql.jdbc.Driver";
		
	
	/*
		public static String dbURL = "jdbc:mysql://localhost:3306/";
		public static String dbName = "rcp";
		public static String username = "root";
		public static String password = "sesame";
		public static String driver = "com.mysql.jdbc.Driver";
	*/
		
	private static LinkedHashMap<Integer, String> week;	
	
	private String source = "";
	private String sourcePath = "/TestFrame.html";
    private String processed = ""; 
	
    public Nexus() {
        super();
        
        week = new LinkedHashMap<Integer, String>();
        defineWeek();
       
      	/*try {
			Class.forName(driver).newInstance();
			connection = (Connection) DriverManager.getConnection(dbURL + dbName, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
    }

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//boolean loggedIn = (Boolean) request.getSession().getAttribute("loggedIn");
		
		//Check Login
		/*if(!(loggedIn)){
			String pathToView = "/WEB-INF/login/Login.jsp";
			
			RequestDispatcher dispatcher =
					request.getRequestDispatcher( pathToView );
				
				dispatcher.forward(request, response);
		}
		else{*/
			
			request.getSession().setAttribute("week", week);
			
			//Check if connection is active
			if(connection == null){
				try {
					Class.forName(driver).newInstance();
					connection = (Connection) DriverManager.getConnection(dbURL + dbName, username, password);
					request.getSession().setAttribute("connection", connection);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else
				try {
					if(connection.isClosed()){
						Class.forName(driver).newInstance();
						connection = (Connection) DriverManager.getConnection(dbURL , username, password);
						request.getSession().setAttribute("connection", connection);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			
			//Development Tool [1]
			//getSource();
			
			//System.out.println(getServletContext().getRealPath("C:\\Users\\Frank Rodriguez\\Desktop\\My Classes\\Senior Year\\Winter 2012\\CS 437\\Project\\Source\\rcp-svn\\WebContent\\NexusTemplate.html")); 
			
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			
			printContent(out);
			
			out.close();
			
			
				//Development Tool [1]
				//transcribe();
				//System.out.println(processed);
				//writeToFile(processed);
				//flushResources();
		
		//}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
	
	private void printContent(PrintWriter out){
		
		out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");

		out.println("<html>");

		out.println("<head>");

		out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");

		out.println("<title>Management Nexus</title>");

		out.println("<link href=\"resources/CSS/NexusTemplate.css\" rel=\"stylesheet\" type=\"text/css\"/>");

		out.println("</head>");

		out.println("<body>");

		out.println("<div class=\"masterDiv\">");

		out.println("<div class=\"container\">");

		out.println("<div class=\"header\">");

		out.println("<h1><a href=\"Blank.html\" target=\"canvas\" class=\"banner\">Restaurant Control Program</a></h1>");

		out.println("<table>");

		out.println("</table>");

		out.println("</div>");

		out.println("<div class=\"brig\">");

		out.println("<iframe src=\"\" name=\"canvas\"></iframe>");

		out.println("</div>");

		out.println("<div class=\"sidebar\">");

		out.println("<table class=\"grid\">");

		out.println("<tr>");

		out.println("<td> <a href=\"CreateEmployee\" target=\"canvas\">Employee Creation</a> </td>");		//Change back to Servlet

		out.println("</tr>");

		out.println("<tr>");

		out.println("<td> <a href=\"EmployeesTable\" target=\"canvas\">Employees</a> </td>");

		out.println("</tr>");

		out.println("<tr>");

		out.println("<td> <a href=\"MenuCreation\" target=\"canvas\">Menu Creation</a> </td>");

		out.println("</tr>");

		out.println("<tr>");

		out.println("<td> <a href=\"MenusTable\" target=\"canvas\">Menu Directory</a> </td>");

		out.println("</tr>");
		
		out.println("<tr>");

		out.println("<td> <a href=\"Reporting\" target=\"canvas\">Reporting</a> </td>");

		out.println("</tr>");

		out.println("</table>");

		out.println("</div>");

		out.println("<div class=\"footer\">");

		out.println("<a href='LogoutServlet'>logout</a>");

		out.println("</div>");

		out.println("</div>");

		out.println("</div>");

		out.println("</body>");

		out.println("</html>");

	}
	
	
/************************************************ File IO **********************************************************/
	
	private void getSource(){
		File file = new File(this.sourcePath);
		BufferedReader reader = null;
		
		if(!file.exists()){
			System.out.println("Source not found.");
			System.out.println("-Program terminated.");
			System.exit(0);
		}
		
		//Read Source HTML
		try {
			reader = new BufferedReader(new FileReader(file));
			Scanner in = new Scanner(reader);
			
			while(in.hasNext()){
				this.source += in.nextLine() + "\n";
			}
			
			reader.close();
			in.close();
		} 
		catch (FileNotFoundException e){
			e.printStackTrace();
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
	
	private void flushResources(){
		this.source = "";
		this.processed = "";
	}
	
	private void writeToFile(String p){
		File file = new File("" +
				"C:\\Users\\Frank Rodriguez\\Desktop\\My Classes\\Senior Year\\Winter 2012\\CS 437\\Project\\Source\\rcp-svn\\WebContent\\Transcribed.txt" +
				"");
		
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(file));
			out.write(p);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private String parseHTML(String s){
		String parsed = "";
	 
		for(int i = 0; i < s.length(); i++){
			if(s.charAt(i) == '"'){
				parsed += "\\\"";
			} 
			else{
				parsed += s.charAt(i);
			}
		}
		
		return parsed;
	}
	
	private void transcribe(){
		Scanner in = new Scanner(source);
		String current = "";
		
		while(in.hasNext()){
			current = in.nextLine();
			current = current.trim();
			
			if(current.length() == 0){
				continue;
			}
			else{
				current = parseHTML(current);
				this.processed += "out.println(\"" + current + "\");" + "\n\n";
			}
		}
		
		in.close();
	}
	
	private static void defineWeek(){
		week.put(1, "Monday");
		week.put(2, "Tuesday");
		week.put(3, "Wednesday");
		week.put(4, "Thursday");
		week.put(5, "Friday");
		week.put(6, "Saturday");
		week.put(7, "Sunday");
	}
	
	public void destroy(){
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		super.destroy();
	}

}
